package com.china08.yxyapi.api.temp;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.entity.db.res.YxyResResource;
import com.china08.yxyapi.entity.es.ElasticYxyResResource;
import com.china08.yxyapi.repository.db.YxyResResourceRepository;
import com.china08.yxyapi.util.EsResourceUpdateUtil;

@CrossOrigin
@RestController
public class ResourceFileSizeUpdController {

    @Autowired
    private ElasticsearchTemplate et;

    @Autowired
    private YxyResResourceRepository yxyResResourceRepository;

    @Value("${weike.storage.upload.video}")
    private String uploadBasePath;

    @Value("${yxy.res.url}")
    private String yxy_res_url;

    /**
     * 2017-05-05/2017-05-06
     * 
     * @param request
     * @param response
     * @param begin
     * @param end
     * @return
     */
    @RequestMapping(value = "/api_yw/temp/resource/filesize", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object process(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("资源处理错误fileSize开始");
        TermQueryBuilder builder = new TermQueryBuilder("fileSize", 10);

        SearchQuery query = new NativeSearchQueryBuilder().withQuery(builder).build();
        long l = et.count(query, ElasticYxyResResource.class);
        Pageable pageable = new PageRequest(0, Integer.parseInt(l + ""));
        SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).withPageable(pageable).build();
        List<ElasticYxyResResource> elasticYxyResResources = et.queryForList(searchQuery, ElasticYxyResResource.class);
        for (ElasticYxyResResource elasticYxyResResource : elasticYxyResResources) {
            String resourceId = elasticYxyResResource.getId();
            System.out.println("正在处理:" + resourceId);
            YxyResResource resource = yxyResResourceRepository.findById(resourceId);
            if (resource != null) {
                try {
                    String string = System.currentTimeMillis() + "_";
                    String string1 = DigestUtils.md5Hex("f0946d11126ef6852241bf811c534023" + resourceId);
                    String AuthParam = string + string1;
                    String downloadUrl = yxy_res_url + "/res/download/" + resourceId + "?AuthParam=" + AuthParam;
                    URL url = new URL(downloadUrl);
                    URLConnection uConnection = url.openConnection();
                    String fileSize = uConnection.getHeaderField("Content-Length");
                    if (StringUtils.isNotBlank(fileSize)) {
                        resource.setFileSize(Integer.parseInt(fileSize));
                        yxyResResourceRepository.save(resource);
                        EsResourceUpdateUtil.updateEsResoure(resourceId);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    continue;
                }

            }
            System.out.println("结束:" + resourceId);
        }
        System.out.println("资源处理错误fileSize结束");
        return "";
    }

}
